home *** CD-ROM | disk | FTP | other *** search
/ QRZ! Ham Radio 4 / QRZ Ham Radio Callsign Database - Volume 4.iso / files / tcpip / amiga / asrc29p.lha / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-12-29  |  14.0 KB  |  690 lines

  1. /* Main network program - provides both client and server functions */
  2. #include <stdio.h>
  3. #include <time.h>
  4. #include <stdarg.h>
  5. #include <dos.h>
  6. #include <stdlib.h>
  7. #include <ios1.h>
  8. #include <proto/dos.h>
  9. #include <ctype.h>
  10.  
  11. #include "global.h"
  12. #include "config.h"
  13. #include "files.h"
  14. #include "mbuf.h"
  15. #include "socket.h"
  16. #include "iface.h"
  17. #include "ftpcli.h"
  18. #include "telnet.h"
  19. #include "remote.h"
  20. #include "session.h"
  21. #include "cmdparse.h"
  22. #include "ax25.h"
  23. #include "kiss.h"
  24. #include "enet.h"
  25. #include "timer.h"
  26. #include "proc.h"
  27. #include "tty.h"
  28. #include "daemon.h"
  29. #include "usock.h"
  30. #include "netrom.h"
  31. #include "ip.h"
  32. #include "tcp.h"
  33. #include "udp.h"
  34. #include "amiga/amiga.h"
  35. #include "commands.h"
  36.  
  37. #include "amiga/windows.h"
  38.  
  39. extern int WBorder;
  40. extern int WResize;
  41. extern int amiga_winsizes[4];
  42. extern long amiga_wintype;
  43.  
  44. extern struct cmds Cmds[],Startcmds[],Stopcmds[],Attab[];
  45. extern int32 Heapsize;
  46.  
  47. static char Escape = 0x1d;    /* default escape character is ^] */
  48.  
  49. char Badhost[] = "Unknown host %s\n";
  50. char Badinterface[] = "Interface \"%s\" unknown\n";
  51.  
  52. char *Hostname;
  53. char Nospace[] = "No space!!\n";    /* Generic malloc fail message */
  54.  
  55. struct proc *Cmdpp;
  56. struct tm *stime;
  57. int Console;
  58. struct proc *Display;
  59. struct session *Command;
  60. static time_t StartTime;        /* Time that NOS was started */
  61. static int Verbose;
  62. extern int Attended;
  63. char *curftwin;
  64.  
  65. static int doelapsed __ARGS((int argc, char *argv[], void *p));
  66.  
  67. int main(argc,argv)
  68. int argc;
  69. char *argv[];
  70. {
  71.     char *inbuf,*intmp;
  72.     FILE *fp;
  73.     struct daemon *tp;
  74.     struct mbuf *bp;
  75.     int rootset, c;
  76.  
  77.     StartTime = time(&StartTime);            /* NOS Start_Up time */
  78.  
  79.     curftwin = "0023";
  80.  
  81.     rootset = 0;
  82.     while((c = getopt(argc,argv,"s:d:m:l:t:w:h:vbr?")) != EOF){
  83.         switch(c) {
  84.         case '?': /* Options HELP */
  85.             printf("\n  AmigaNOS Command-Line Options:\n\n");
  86.             printf("  -l<nnn>     Left edge     nnn - pixels\n");
  87.             printf("  -t<nnn>     Top Edge      nnn - pixels\n");
  88.             printf("  -w<nnn>     Width         nnn - pixels\n");
  89.             printf("  -h<nnn>     Height        nnn - pixels\n\n");
  90.             printf("  -b          Borderless Windows\n");
  91.             printf("  -r          Window Resize Gadgets\n\n");
  92.             printf("  -s<nn>      Number of sockets\n");
  93.             printf("  -d<sssssss> AmigaNOS Root device/directory\n");
  94.             printf("  -m<nnn>     Heap memory size\n\n");
  95.             printf("  -v          Verbose flag\n\n");
  96.             exit(0);
  97.         case 'l': /* Left edge */
  98.             amiga_winsizes[0] = atoi(optarg);
  99.             break;
  100.         case 't': /* Top edge */
  101.             amiga_winsizes[1] = atoi(optarg);
  102.             break;
  103.         case 'w': /* Width */
  104.             amiga_winsizes[2] = atoi(optarg);
  105.             break;
  106.         case 'h': /* Height */
  107.             amiga_winsizes[3] = atoi(optarg);
  108.             break;
  109.         case 'b': /* Border */
  110.             WBorder = 0;
  111.             break;
  112.         case 'r': /* Resize */
  113.             WResize = 0;
  114.             break;
  115.         case 's': /* Number of sockets */
  116.             Nusock = atoi(optarg);
  117.             break;
  118.         case 'd': /* Root directory for various files */
  119.             initroot(optarg);
  120.             rootset = 1;
  121.             break;
  122.         case 'm': /* Heap memory size */
  123.             Heapsize = 1024 * atol(optarg);
  124.             break;
  125.         case 'v': /* Verbose flag */
  126.             Verbose = 1;
  127.             break;
  128.         }
  129.     }
  130.     if(!rootset) {
  131.         initroot("TCPIP");
  132.     }
  133.     kinit();
  134.     ioinit(Heapsize);
  135.     sockinit();
  136.     Cmdpp = mainproc("cmdintrp");
  137.     mainlog(-1,"Start NOS v%s", Version);
  138.  
  139.     Sessions = (struct session *)callocw(Nsessions,sizeof(struct session));
  140.     Command = Lastcurr = newsession("command interpreter",COMMAND,0);
  141.     Command->flowmode = 1;        /* set 'more' paging on command screen */
  142.     Display = newproc("display",250,display,0,NULLCHAR,NULL);
  143.     tprintf("KA9Q Internet Protocol Package, v%s\n",Version);
  144.     tprintf("Copyright 1989 by Phil Karn, KA9Q\n");
  145.     fflush(stdout);
  146.  
  147.     if(optind < argc){
  148.         /* Read startup file named on command line */
  149.         if((fp = fopen(argv[optind],READ_TEXT)) == NULLFILE)
  150.             tprintf("Can't read config file %s: %s\n",
  151.              argv[optind],sys_errlist[errno]);
  152.     } else {
  153.         fp = fopen(Startup,READ_TEXT);
  154.     }
  155.     if(fp != NULLFILE){
  156.         inbuf = mallocw(BUFSIZ);
  157.         intmp = mallocw(BUFSIZ);
  158.         while(fgets(inbuf,BUFSIZ,fp) != NULLCHAR){
  159.             strcpy(intmp,inbuf);
  160.             if(Verbose)
  161.                 tprintf("%s",intmp);
  162.             if(cmdparse(Cmds,inbuf,NULL) != 0){
  163.                 tprintf("input line: %s",intmp);
  164.             }
  165.         }
  166.         fclose(fp);
  167.         free(inbuf);
  168.         free(intmp);
  169.     }
  170.     /* Start background Daemons */
  171.     for(tp=Daemons;;tp++){
  172.         if(tp->name == NULLCHAR)
  173.             break;
  174.         newproc(tp->name,tp->stksize,tp->fp,0,NULLCHAR,NULL);
  175.     }
  176.     /* Now loop forever, processing commands */
  177.     for(;;){
  178.                 long currtime;
  179.                 time(&currtime);
  180.                 stime = localtime(&currtime);
  181.                 tprintf("\x0f\x1b[32mNOS <\x1b[33m%02d:%02d - %s\x1b[32m> \x1b[0m",
  182.                         stime->tm_hour, stime->tm_min, Hostname);
  183.         usflush(Command->output);
  184.         if(recv_mbuf(Command->input,&bp,0,NULLCHAR,0) != -1){
  185.             (void)cmdparse(Cmds,bp->data,Lastcurr);
  186.             free_p(bp);
  187.         }
  188.     }
  189. }
  190.  
  191. /* Keyboard input process */
  192. void keyboard(i,v1,v2)
  193. int i;
  194. void *v1;
  195. void *v2;
  196. {
  197.     int c;
  198.     struct mbuf *bp;
  199.  
  200.     /* Keyboard process loop */
  201.     for(;;){
  202.         c = kbread();
  203.         if(c == Escape && Escape != 0)
  204.             c = -2;
  205.         if(c == -2 && Current != Command){
  206.             /* Save current tty mode and set cooked */
  207.             swapscreen(Current,Command);
  208.             Lastcurr = Current;
  209.             Current = Command;
  210.             /* set 'more' paging on command screen */
  211.             Command->flowmode = 1;
  212.         }
  213.         Current->row = MOREROWS;
  214.         psignal(&Current->row,1);
  215.         if(c >= 0){
  216.             /* If the screen driver was in morewait state, this char
  217.              * has woken him up. Toss it so it doesn't also get taken
  218.              * as normal input. If the char was a command escape,
  219.              * however, it will be accepted; this gives the user
  220.              * a way out of lengthy output.
  221.              */
  222.             if(!Current->morewait
  223.              && (bp = ttydriv(Current,(char)c)) != NULLBUF){
  224.                 if(Current->record != NULLFILE
  225.                  && Current->ttystate.echo)
  226.                     write_p(Current->record,bp);
  227.                 send_mbuf(Current->input,bp,0,NULLCHAR,0);
  228.             }
  229.         }
  230.     }
  231. }
  232.  
  233. static int doelapsed(argc, argv, p)
  234. int argc;
  235. char *argv[];
  236. void *p;
  237. {
  238.     time_t nowtime, elapsedtime;
  239.     unsigned int days,hrs,mins,secs;
  240.  
  241.     nowtime = time(&nowtime);            /* current time */
  242.     elapsedtime = nowtime - StartTime;        /* nos elapsed time */
  243.     secs = elapsedtime % 60;
  244.     elapsedtime = elapsedtime / 60;
  245.     mins = elapsedtime % 60;
  246.     elapsedtime = elapsedtime / 60;
  247.     hrs = elapsedtime % 24;
  248.     elapsedtime = elapsedtime / 24;
  249.     days = elapsedtime;
  250.     tprintf("Elapsed time => %u days: %02u hours: %02u minutes: %02u seconds.\n\n",days,hrs,mins,secs);
  251.  
  252.     return 0;
  253. }
  254.  
  255. int doexit(argc,argv,p)
  256. int argc;
  257. char *argv[];
  258. void *p;
  259. {
  260.     if(keywait("Are you sure ? ", 0) == 'y') {
  261.         doelapsed(NULL, NULLCHAR, NULL);
  262.         mainlog(-1,"Stop  NOS v%s", Version);
  263.         reset_all();
  264.         iostop();
  265.         exit(0);
  266.     }
  267.     return 0;    /* To satisfy lint */
  268. }
  269.  
  270. int dohostname(argc,argv,p)
  271. int argc;
  272. char *argv[];
  273. void *p;
  274. {
  275.     if(argc < 2)
  276.         tprintf("%s\n",Hostname);
  277.     else {
  278.         if(Hostname != NULLCHAR)
  279.             free(Hostname);
  280.         Hostname = strdup(argv[1]);
  281.     }
  282.     return 0;
  283. }
  284.  
  285. int dohelp(argc,argv,p)
  286. int argc;
  287. char *argv[];
  288. void *p;
  289. {
  290.     register struct cmds *cmdp;
  291.     int i;
  292.     char buf[77];
  293.  
  294.     if(*argv[0] == '?' ) {
  295.         tprintf("Main commands:\n");
  296.         memset(buf,' ',sizeof(buf));
  297.         buf[75] = '\n';
  298.         buf[76] = '\0';
  299.         cmdp = Cmds;    /* Skip initial NULL */
  300.         cmdp++;        /* Skip initial NULL */
  301.         for(i=0;cmdp->name != NULL;cmdp++,i = (i+1)%5) {
  302.             strncpy(&buf[i*15],cmdp->name,strlen(cmdp->name));
  303.             if(i == 4){
  304.                 tprintf(buf);
  305.                 memset(buf,' ',sizeof(buf));
  306.                 buf[75] = '\n';
  307.                 buf[76] = '\0';
  308.             }
  309.         }
  310.         if(i != 0)
  311.             tprintf(buf);
  312.         return 0;
  313.     } else {
  314.         buf[0] = '\0';
  315.         if(argc > 1)
  316.             for(i=0; Cmds[i].name != NULLCHAR; ++i)
  317.                 if(!strncmp(Cmds[i].name,
  318.                     argv[1],strlen(argv[1]))) {
  319.                     sprintf(buf,"RUN LESS %s/main.%s",
  320.                         Helpdir,Cmds[i].name);
  321.                     if (!Execute(buf, 0, 0)) {
  322.                         return (FILE *)NULL;
  323.                     }
  324.                     break;
  325.                 }
  326.         return 0;
  327.     }
  328. }
  329.  
  330. /* Attach an interface
  331.  * Syntax: attach <hw type> <I/O address> <vector> <mode> <label> <bufsize> [<speed>]
  332.  */
  333. int doattach(argc,argv,p)
  334. int argc;
  335. char *argv[];
  336. void *p;
  337. {
  338.     return subcmd(Attab,argc,argv,p);
  339. }
  340.  
  341. /* Manipulate I/O device parameters */
  342. int doparam(argc,argv,p)
  343. int argc;
  344. char *argv[];
  345. void *p;
  346. {
  347.     register struct iface *ifp;
  348.  
  349.     if((ifp = if_lookup(argv[1])) == NULLIF){
  350.         tprintf(Badinterface,argv[1]);
  351.         return 1;
  352.     }
  353.     if(ifp->ioctl == NULLFP){
  354.         tprintf("Not supported\n");
  355.         return 1;
  356.     }
  357.     /* Pass rest of args to device-specific code */
  358.     return (*ifp->ioctl)(ifp,argc-2,argv+2);
  359. }
  360.  
  361. /* Display or set IP interface control flags */
  362. int domode(argc,argv,p)
  363. int argc;
  364. char *argv[];
  365. void *p;
  366. {
  367.     register struct iface *ifp;
  368.  
  369.     if((ifp = if_lookup(argv[1])) == NULLIF) {
  370.         tprintf(Badinterface, argv[1]);
  371.         return 1;
  372.     }
  373.     if(argc < 3){
  374.         tprintf("%s: %s\n",ifp->name,
  375.          (ifp->flags & CONNECT_MODE) ? "VC mode" : "Datagram mode");
  376.         return 0;
  377.     }
  378.     switch(argv[2][0]){
  379.     case 'v':
  380.     case 'c':
  381.     case 'V':
  382.     case 'C':
  383.         ifp->flags = CONNECT_MODE;
  384.         break;
  385.     case 'd':
  386.     case 'D':
  387.         ifp->flags = DATAGRAM_MODE;
  388.         break;
  389.     default:
  390.         tprintf("Usage: %s [vc | datagram]\n",argv[0]);
  391.         return 1;
  392.     }
  393.     return 0;
  394. }
  395.  
  396. int doescape(argc,argv,p)
  397. int argc;
  398. char *argv[];
  399. void *p;
  400. {
  401.     if(argc < 2)
  402.         tprintf("0x%x\n",Escape);
  403.     else 
  404.         Escape = *argv[1];
  405.     return 0;
  406. }
  407.  
  408. /* Generate system command packet. Synopsis:
  409.  * remote [-p port#] [-k key] [-a hostname] <hostname> reset|exit|kickme
  410.  */
  411. int doremote(argc,argv,p)
  412. int argc;
  413. char *argv[];
  414. void *p;
  415. {
  416.     struct sockaddr_in fsock;
  417.     int s,c;
  418.     char *data,x;
  419.     int16 port,len;
  420.     char *key = NULLCHAR;
  421.     int klen;
  422.     int32 addr = 0;
  423.     char *cmd,*host;
  424.  
  425.     port = IPPORT_REMOTE;    /* Set default */
  426.     optind = 1;        /* reinit getopt() */
  427.     while((c = getopt(argc,argv,"a:p:k:s:")) != EOF){
  428.         switch(c){
  429.         case 'a':
  430.             addr = resolve(optarg);
  431.             break;
  432.         case 'p':
  433.             port = atoi(optarg);
  434.             break;
  435.         case 'k':
  436.             key = optarg;
  437.             klen = strlen(key);
  438.             break;
  439.         case 's':
  440.             Rempass = strdup(optarg);
  441.             return 0;    /* Only set local password */
  442.         }
  443.     }
  444.     if(optind > argc - 2){
  445.         tprintf("Insufficient args\n");
  446.         return -1;
  447.     }
  448.     host = argv[optind++];
  449.     cmd = argv[optind];
  450.     if((s = socket(AF_INET,SOCK_DGRAM,0)) == -1){
  451.         tprintf("socket failed\n");
  452.         return 1;
  453.     }
  454.     len = 1;
  455.     /* Did the user include a password or kickme target? */
  456.     if(addr != 0)
  457.         len += sizeof(int32);
  458.  
  459.     if(key != NULLCHAR)
  460.         len += klen;
  461.  
  462.     if(len == 1)
  463.         data = &x;
  464.     else
  465.         data = mallocw(len);
  466.  
  467.     fsock.sin_family = AF_INET;
  468.     fsock.sin_addr.s_addr = resolve(host);
  469.     fsock.sin_port = port;
  470.  
  471.     switch(cmd[0]){
  472.     case 'r':
  473.         data[0] = SYS_RESET;
  474.         if(key != NULLCHAR)
  475.             strncpy(&data[1],key,klen);
  476.         break;
  477.     case 'e':
  478.         data[0] = SYS_EXIT;
  479.         if(key != NULLCHAR)
  480.             strncpy(&data[1],key,klen);
  481.         break;
  482.     case 'k':
  483.         data[0] = KICK_ME;
  484.         if(addr != 0)
  485.             put32(&data[1],addr);
  486.         break;
  487.     default:
  488.         tprintf("Unknown command %s\n",cmd);
  489.         goto cleanup;
  490.     }
  491.     /* Form the command packet and send it */
  492.     if(sendto(s,data,len,0,(char *)&fsock,sizeof(fsock)) == -1){
  493.         tprintf("sendto failed: %s\n",sys_errlist[errno]);
  494.         goto cleanup;
  495.     }
  496. cleanup:
  497.     if(data != &x)
  498.         free(data);
  499.     close_s(s);
  500.     return 0;
  501. }
  502.  
  503. int doless(argc,argv,p)
  504. int argc;
  505. char *argv[];
  506. void *p;
  507. {
  508.     char buf[81];
  509.  
  510.     sprintf(buf,"RUN LESS %s",argv[1]);
  511.     if (!Execute(buf, 0, 0)) {
  512.         return (FILE *)NULL;
  513.     }
  514.     return 0;
  515. }
  516.  
  517. /* Log messages of the form
  518.  * 00:00:00 44.64.0.7:1003 open FTP
  519.  */
  520. void mainlog(int s,char *fmt, ...)
  521. {
  522.     va_list ap;
  523.     char *cp, *da, *mo, *yr, *ti, ML[128];
  524.     long t;
  525.     int i;
  526.     struct sockaddr fsocket;
  527.     FILE *fp;
  528.  
  529.     time(&t);
  530.     cp = ctime(&t);
  531.     rip(cp);
  532.  
  533.     strtok(cp," ");    /* Day   */
  534.     mo = strtok(NULL," ");  /* Month */
  535.     da = strtok(NULL," ");  /* Date  */
  536.     ti = strtok(NULL," ");  /* Time  */
  537.     yr = strtok(NULL," ");  /* Year  */
  538.  
  539.     sprintf(ML, "%s/%s-%s-%s", LogsDir, mo, da, yr);
  540.  
  541.     if((fp = fopen(ML,APPEND_TEXT)) == NULLFILE)
  542.         return;
  543.  
  544.     i = SOCKSIZE;
  545.  
  546.     fprintf(fp,"%s ",ti);
  547.     if(getpeername(s,(char *)&fsocket,&i) != -1)
  548.         fprintf(fp," %s",psocket(&fsocket));
  549.     fprintf(fp," - ");
  550.     va_start(ap,fmt);
  551.     vfprintf(fp,fmt,ap);
  552.     va_end(ap);
  553.     fprintf(fp,"\n");
  554.     fclose(fp);
  555. }
  556.  
  557. int dosource(argc,argv,p)
  558. int argc;
  559. char *argv[];
  560. void *p;
  561. {
  562.     int linenum = 0;
  563.     char *inbuf,*intmp;
  564.     FILE *fp;
  565.  
  566.     /* Read command source file */
  567.     if((fp = fopen(argv[1],READ_TEXT)) == NULLFILE){
  568.         tprintf("Can't read source file %s: %s\n",
  569.          argv[1],sys_errlist[errno]);
  570.         return 1;
  571.     }
  572.  
  573.     inbuf = malloc(BUFSIZ);
  574.     intmp = malloc(BUFSIZ);
  575.     while(fgets(inbuf,BUFSIZ,fp) != NULLCHAR){
  576.         strcpy(intmp,inbuf);
  577.         linenum++;
  578.         if(Verbose)
  579.             tprintf("%s",intmp);
  580.         if(cmdparse(Cmds,inbuf,NULL) != 0){
  581.             tprintf("*** file \"%s\", line %d: %s\n",
  582.                 argv[1],linenum,intmp);
  583.         }
  584.     }
  585.     fclose(fp);
  586.     free(inbuf);
  587.     free(intmp);
  588.     return 0;
  589. }
  590.  
  591. dostatus(argc,argv,p)
  592. int argc;
  593. char *argv[];
  594. void *p;
  595. {
  596.     tprintf("KA9Q Internet Protocol Package, v%s\n\n",Version);
  597.     tprintf("NOS was started on %s\n", ctime(&StartTime));
  598.     doelapsed(NULL, NULLCHAR, NULL);
  599.     tprintf("The station is currently %sttended.\n", Attended ? "A" : "Una");
  600.     tprintf("\nNOS configuration information.\ncontaining ");
  601. #ifdef SERVERS
  602.     tprintf(" TCP servers,");
  603. #endif
  604. #ifdef POP2
  605.     tprintf(" POP2,");
  606. #endif
  607. #ifdef POP3
  608.     tprintf(" POP3,");
  609. #endif
  610. #ifdef NNTP
  611.     tprintf(" NNTP,");
  612. #endif
  613. #ifdef RIP
  614.     tprintf(" RIP,");
  615. #endif
  616. #ifdef RSPF
  617.     tprintf(" RSPF,");
  618. #endif
  619. #ifdef    BOOTP
  620.     tprintf(" BOOTP,");
  621. #endif
  622. #ifdef HOPCHECK
  623.     tprintf(" HOP,");
  624. #endif
  625. #ifdef MAILBOX
  626.     tprintf(" MAILBOX");
  627. #endif
  628. #ifdef    ASY
  629.     tprintf("\nGeneric async interface\n");
  630. #endif
  631. #ifdef    SLIP
  632.     tprintf("SLIP async interface\n");
  633. #endif
  634. #ifdef    KISS
  635.     tprintf("KISS async interface\n");
  636. #endif
  637. #ifdef    NRS
  638.     tprintf("NET/ROM async interface\n");
  639. #endif
  640. #ifdef    NETROM
  641.     tprintf("NET/ROM network interface\n");
  642. #endif
  643. #ifdef DIGGER
  644.     tprintf("Digger by WA2ZZX/G1YYH\n");
  645. #endif
  646. #ifdef    CHATNODE
  647.     tprintf("ChatNode by DK5SG/G1YYH\n");
  648. #endif
  649. #ifdef    PPP
  650.     tprintf("Point to Point network interface\n");
  651. #endif
  652. #ifdef    VJCOMPRESS
  653.     tprintf("Van Jacobson compression on SLIP/PPP\n");
  654. #endif
  655. #ifdef    FORTH
  656.     tprintf("Forth programming language\n");
  657. #endif
  658.     return 0;
  659. }
  660.  
  661. /* Set up FTP/Download window */
  662. int doftpdown(argc,argv,p)
  663. int argc;
  664. char *argv[];
  665. void *p;
  666. {
  667.     if(argc == 1)
  668.         tprintf("Current FTP/Download Window = %s\n", curftwin);
  669.     else {
  670.         strcpy(curftwin,argv[1]);
  671.         tprintf("New FTP/Download Window = %s \n", curftwin);
  672.     }
  673.     return 0;
  674. }
  675.  
  676. #ifdef    STIMEOUT
  677. static struct cmds TOtab[] = {
  678.     "ftp",        doftptdisc,    0, 0, NULLCHAR,
  679.     NULLCHAR,    NULLFP,        0, 0, "Huh ?",
  680. };
  681.  
  682. int dotimeout(argc,argv,p)
  683. int argc;
  684. char *argv[];
  685. void *p;
  686. {
  687.     return subcmd(TOtab,argc,argv,p);
  688. }
  689. #endif
  690.